import { _decorator, Component, EventTouch, Node, Sprite } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Talent')
export class Talent extends Component {
    private cb: Function;
    private target: any;
    /**是否激活 */
    private _isActivated: boolean = true;
    public get isActivated(): boolean {
        return this._isActivated;
    }
    public set isActivated(value: boolean) {
        this._isActivated = value;

        this.node.getComponentsInChildren(Sprite).forEach(s => {
            s.grayscale = !value;
        });
    }

    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this, true);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this, true);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this, true);
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }
    start() {

    }

    update(deltaTime: number) {

    }

    onTouchStart(event: EventTouch) {
        if (!this.isActivated) return;
    }

    onTouchEnd(event: EventTouch) {
        if (!this.isActivated) return;
        this.cb && this.cb.apply(this.target);

    }
    onTouchCancel(event: EventTouch) {
        if (!this.isActivated) return;
        this.cb && this.cb.apply(this.target);
    }

    onClick(cb: Function, target: any) {
        this.cb = cb;
        this.target = target;

    }

}


